import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/flutter/_15%E7%BB%BC%E5%90%88%E7%BB%83%E4%B9%A0/mixin%E5%BA%94%E7%94%A8/base_page.dart';
import 'package:flutter_study/flutter/_15%E7%BB%BC%E5%90%88%E7%BB%83%E4%B9%A0/mixin%E5%BA%94%E7%94%A8/base_state.dart';

/// 统一错误处理,遇到错误弹出Snackbar
mixin ErrorHandleMixin<Page extends BasePage> on BaseState<Page> {
  @override
  void initState() {
    super.initState();

    widget.bloc.errorStream.listen((event) {
      showErrorSnackBar(event, scaffoldKey.currentState);
    });
  }

  void showErrorSnackBar(String event, ScaffoldState context) {
    print('receive error $event');
    if (event != null) {
      context.showSnackBar(SnackBar(content: Text(event)));
    }
  }
}
